﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using eCommerce.Core;
using eCommerce.Core.Domain.Customers;
using eCommerce.Core.Domain.Payments;
using eCommerce.Models.Checkouts;
using eCommerce.Services.Orders;
using eCommerce.Services.Payments;
using eCommerce.Services.ShoppingCarts;

namespace eCommerce.Controllers.api
{
    public class CheckoutApiController : ApiController
    {
        private IWorkContext _workContext;
        private IOrderProcessingService _orderProcessingService;
        private IPaymentService _paymentService;
        private IShoppingCartService _shoppingCartService;

        public CheckoutApiController(IWorkContext workContext, IShoppingCartService shoppingCartService, IPaymentService paymentService, IOrderProcessingService orderProcessingService)
        {
            _workContext = workContext;
            _shoppingCartService = shoppingCartService;
            _paymentService = paymentService;
            _orderProcessingService = orderProcessingService;
        }

        public ConfirmOrderResult ConfirmOrder(ConfirmOrderRequest request)
        {
            var result = new ConfirmOrderResult();
            //validation
            var cart = _shoppingCartService.GetShoppingCart(_workContext.CurrentCustomer.Id);
            //_workContext.CurrentCustomer.ShoppingCartItems.ToList();
            if (cart.Count == 0)
            {
                result.Status = "1";
                result.Message = "cart is empty";
                return result;
            }



            if (_workContext.CurrentCustomer.IsGuest())
            {
                result.Status = "1";
                result.Message = "unauthorized";
                return result;
            }


            
            try
            {

                //place order
                var processPaymentRequest = new ProcessPaymentRequest
                {
                    CustomerId = _workContext.CurrentCustomer.Id,
                    PaymentMethodSystemName = _workContext.CurrentCustomer.SelectedPaymentMethodSystemName
                };
                var placeOrderResult = _orderProcessingService.PlaceOrder(processPaymentRequest);
                if (placeOrderResult.Success)
                {
                    //_httpContext.Session["OrderPaymentInfo"] = null;
                    var postProcessPaymentRequest = new PostProcessPaymentRequest()
                    {
                        Trading = placeOrderResult.PlacedTrading
                    };
                    _paymentService.PostProcessPayment(postProcessPaymentRequest);

                    result.Status = "0";
                   


                }
                else
                {
                    result.Status = "1";
                    result.Message = placeOrderResult.Errors[0];
                }
            }
            catch (Exception exc)
            {
                result.Status = "1";
                result.Message = exc.Message;
            }

            return result;
        }
    }

    public class ConfirmOrderRequest
    {
        public IList<ShoppingCartItemModel> Items { get; set; }
        public string PaymethodName { get; set; }

        public int SelectedShippingAddressId { get; set; }
    }

    public class ConfirmOrderResult
    {
        public string Status { get; set; }
        public string Message { get; set; }
    }
}
